﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EAD_CA3.Models;

namespace EAD_CA3.Controllers
{ 
    public class LeagueController : Controller
    {
        private EADMiniProjectEntities db = new EADMiniProjectEntities();

        // GET: /League/
        [HttpGet]
        public ViewResult Index()
        {
            return View(db.FixtureDatas.ToList());
        }

        // GET: /League/About
        [HttpGet]
        public ActionResult About()
        {
            return View();
        }

        // GET: /League/Details/5
        [HttpGet]
        public ViewResult Details(int id)
        {
            FixtureData fixturedata = db.FixtureDatas.Single(f => f.ID == id);
            return View(fixturedata);
        }

        // GET: /League/Create
        [HttpGet]
        public ActionResult Create()
        {
            return View();
        } 

        // POST: /League/Create
        [HttpPost]
        public ActionResult Create(FixtureData fixturedata)
        {
            if (ModelState.IsValid)
            {
                db.FixtureDatas.AddObject(fixturedata);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(fixturedata);
        }
        
        // GET: /League/Edit/5
        [HttpGet]
        public ActionResult Edit(int id)
        {
            FixtureData fixturedata = db.FixtureDatas.Single(f => f.ID == id);
            return View(fixturedata);
        }

        // POST: /League/Edit/5
        [HttpPost]
        public ActionResult Edit(FixtureData fixturedata)
        {
            if (ModelState.IsValid)
            {
                db.FixtureDatas.Attach(fixturedata);
                db.ObjectStateManager.ChangeObjectState(fixturedata, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(fixturedata);
        }

        // GET: /League/Delete/5
        [HttpGet]
        public ActionResult Delete(int id)
        {
            FixtureData fixturedata = db.FixtureDatas.Single(f => f.ID == id);
            return View(fixturedata);
        }

        // POST: /League/Delete/5
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            FixtureData fixturedata = db.FixtureDatas.Single(f => f.ID == id);
            db.FixtureDatas.DeleteObject(fixturedata);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        // GET: /League/Filter
        [HttpGet]
        public ActionResult Filter()
        {
            ViewData["Filter"] = new SelectList(FixtureData.FilterList);
            ViewData["Counties"] = new SelectList(FixtureData.CountyList);
            ViewData["Divisions"] = new SelectList(FixtureData.DivisionList);
            ViewData["Fixtures"] = new SelectList(FixtureData.FixtureDateList);
            return View();
        }

        [HttpPost]
        public ActionResult Filter(String Filter, String Counties, String Divisions, DateTime Fixtures)
        {
            FixtureData f = new FixtureData();
            if (Filter == "Specify a County and retrieve their Division")
            {
                ViewBag.Team = Counties;
                return View("QueryTeamDivision", f.QueryTeamDivision(Counties));
            }
            else if (Filter == "Specify a Division and retrieve all Counties in that Division")
            {
                ViewBag.League = Divisions;
                return View("QueryAllTeamsInADivision", f.QueryAllTeamsInADivision(Divisions));
            }
            else if (Filter == "Specify a Division and retrieve all Fixtures in that Division")
            {
                ViewBag.League = Divisions;
                return View("QueryFixturesByDivision", f.QueryFixturesByDivision(Divisions));
            }
            else if (Filter == "Find all Home and Away Fixtures for a specified County")
            {
                ViewBag.Team = Counties;
                return View("QueryFixturesByTeam", f.QueryFixturesByTeam(Counties));
            }
            else if (Filter == "Find all Home Fixtures for a specified County")
            {
                ViewBag.Team = Counties;
                return View("QueryFixturesByHomeTeam", f.QueryFixturesByHomeTeam(Counties));
            }
            else if (Filter == "Find all Away Fixtures for a specified County")
            {
                ViewBag.Team = Counties;
                return View("QueryFixturesByAwayTeam", f.QueryFixturesByAwayTeam(Counties));
            }
            else if (Filter == "Find all Fixtures on a Specified Date")
            {
                ViewBag.Date = Fixtures;
                return View("QueryFixturesByDate", f.QueryFixturesByDate(Fixtures));
            }
            else
            {
                return View("Index", db.FixtureDatas.ToList());
            }
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}